package org.lq.ht.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.lq.ht.dao.impl.GoodsDaoImpl;
import org.lq.ht.entity.Goods;
import org.lq.ht.service.GoodsService;
/**
 * 商品业务层实现
 * @author 张冲
 * @qq2865346830
 * @package org.lq.ht.service.impl
 * @date 2020年8月21日下午7:59:07
 */
public class GoodsServiceImpl implements GoodsService{
	GoodsDaoImpl gd = new GoodsDaoImpl();
	@Override
	public boolean saveGoods(Goods t) {
		
		return gd.save(t)>0;
	}

	@Override
	public boolean updateGoods(Goods t) {
		// TODO Auto-generated method stub
		return gd.update(t)>0;
	}

	@Override
	public boolean deleteGoodsById(int id) {
		// TODO Auto-generated method stub
		return gd.delete(id)>0;
	}

	@Override
	public List<Goods> findAll() {
		// TODO Auto-generated method stub
		return gd.findAll();
	}

	@Override
	public Goods getById(int id) {
		// TODO Auto-generated method stub
		return gd.getById(id);
	}

	@Override
	public List<Goods> query(String name) {//正则表达式查询
		List<Goods> list = new ArrayList<Goods>();
		String codePattern = "\\d{13}";
		String pinYinPattern = "\\w+";
		String chinesePattern = "[\\u4e00-\\u9fa5].*";
		if(Pattern.matches(codePattern, name)) {
			//条形码查询
			list=gd.getByNumber(name);
			System.out.println("条形码查询");
		}else if(Pattern.matches(pinYinPattern, name)) {
			//拼音查询
			list=gd.getByPinYin(name);
			System.out.println("拼音查询");
		}else if(Pattern.matches(chinesePattern, name)) {
			//商品名称查询
			list=gd.getByName(name);
			System.out.println("名称查询");
		}else {
			list=findAll();
		}
		return list;
	}
	
	@Override
	public Goods getBynumber(int goodsNumber) {
		// TODO Auto-generated method stub
		return gd.getByNumber(goodsNumber);
	}

}
